﻿using System;
using System.Linq;
using System.ComponentModel.Composition;
using System.IO;
using System.Globalization;

using SharpGL.Media3D;
using SharpGL.Enumerations;
using SharpGL.SceneGraph;
using SharpGL.SceneGraph.Primitives;
using SharpGL.SceneGraph.Primitives.SmartAxies;
using SharpGL.SceneGraph.Effects;
using SharpGL.SceneGraph.Cameras;
using SharpGL.SceneGraph.Lighting;

namespace SharpGL.Serialization
{
    //[Export(typeof(IFileFormat))]
    public partial class SceneFileFormat : IFileFormat
    {
        private string normalizeLine(string arg)
        {
            while (arg.IndexOf("  ") != -1) arg = arg.Replace("  ", " ");
            return arg;
        }
        public Scene LoadData(string path)
        {
            Scene scene = new Scene();
            SmartAxies sceneObjectsRoot = new SmartAxies();
            
            /*// Create a set of scene attributes.
            OpenGLAttributesEffect sceneAttributes = new OpenGLAttributesEffect()
            {
                Name = "Scene attributes"
            };
            // Specify the scene attributes.
            sceneAttributes.AttributesGroup.EnableAttributes.EnableDepthTest = true;
            sceneAttributes.AttributesGroup.EnableAttributes.EnableNormalize = true;
            sceneAttributes.AttributesGroup.EnableAttributes.EnableLighting = true;
            sceneAttributes.AttributesGroup.EnableAttributes.EnableTexture2D = false;
            sceneAttributes.AttributesGroup.LightingAttributes.TwoSided = true;
            sceneAttributes.AttributesGroup.ColorBufferAttributes.EnableBlend = false;
            
            // Add effect for all scene
            scene.SceneContainer.Effects.Add(sceneAttributes);*/

            // Add root for all new scene objects
            scene.SceneContainer.Children.Add(sceneObjectsRoot);

            using (StreamReader reader = new StreamReader(path))
            {
                string line = null;                
                CultureInfo cultureInfo = new CultureInfo("ru");
                cultureInfo.NumberFormat.NumberDecimalSeparator = ".";
                while ((line = reader.ReadLine()) != null)
                {
                    line = normalizeLine(line);
                    if (line.StartsWith("OBJECT"))
                    {
                        ScnnFileRecord obj = null;
                        switch (line.Split(new char[] { ' ' }).ElementAt(1))
                        {
                            case "LINE":  
                                obj = new LINE();                                
                                break;
                            case "RASTR":
                                obj = new RASTR()
                                {
                                    path = path
                                };                                                                                      
                                break;
                            case "ZONE":
                                obj = new ZONE();                                
                                break;

                            default: 
                                continue;
                        }
                        if (obj != null)
                        {
                            while (!(line = reader.ReadLine()).StartsWith("END"))
                            {
                                if (line.StartsWith("OBJECT"))
                                {
                                    break;
                                }
                                obj.ParseLine(line);
                            }
                            sceneObjectsRoot.Children.Add(obj.SceneElement);
                        }
                    }
                }
            }
            return scene;
        }

        public bool SaveData(Scene scene, string path)
        {
            throw new NotImplementedException();
        }

        public string[] FileTypes
        {
            get { return new string[] { "scnn" }; }
        }

        public string Filter
        {
            get { return "Scene Files (*.scnn)|*.scnn"; }
        }
    }
}
